package com.ghbank.rdws.common.db.utils;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import com.ghbank.rdws.common.db.LowerCaseColumnMapRowMapper;
import com.ghbank.rdws.common.entity.SysDataSourceConfig;
import com.ghbank.rdws.common.exception.RtdsException;
import com.ghbank.rdws.common.model.DataSourceConfig;
import com.ghbank.rdws.common.model.DbType;

/**
 * 
 * @author liuxudong
 * @date 2019年3月21日
 */
public class DbUtils {
    
    public static List<Map<String,Object>> queryForList(DataSource dataSource,String sql,Map<String,Object> paramMap) {
        final NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
        return namedParameterJdbcTemplate.query(sql, paramMap,new LowerCaseColumnMapRowMapper());
    }
    
    public static Map<String,Object> queryForObject(DataSource dataSource,String sql,Map<String,Object> paramMap) {
        final NamedParameterJdbcTemplate namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
        return namedParameterJdbcTemplate.queryForObject(sql, paramMap,new LowerCaseColumnMapRowMapper());
    }
    
    /**
     * 根据DataSource判断一下数据库类型
     */
    public static DbType judgeDbType(DataSource dataSource) {
        final JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        return (DbType) jdbcTemplate.execute(new ConnectionCallback() {

            public Object doInConnection(Connection c) throws SQLException, DataAccessException {
                DatabaseMetaData meta = c.getMetaData();
                String databaseName = meta.getDatabaseProductName();
                String version = meta.getDatabaseProductVersion();

                if (StringUtils.startsWithIgnoreCase(databaseName, "oracle")) {
                    return DbType.ORACLE;
                } else if (StringUtils.startsWithIgnoreCase(databaseName, "mysql")) {
                    if (StringUtils.contains(version, "-TDDL-")) {
                        return DbType.DRDS;
                    } else {
                        return DbType.MYSQL;
                    }
                } else {
                    throw new RtdsException("unknow database type " + databaseName);
                }
            }
        });

    }
    
    public static DataSourceConfig getDataSourceConfig(SysDataSourceConfig dataSourceConfig,DbType dbType) {
        DataSourceConfig config = new DataSourceConfig();
        config.setUsername(dataSourceConfig.getJdbcUsername());
        config.setPassword(dataSourceConfig.getJdbcPassword());
        config.setUrl(dataSourceConfig.getJdbcUrl());
        config.setEncode("UTF-8");
        config.setType(dbType);
        return config;
    }
}
